package dao.impl;

import dao.IPayWayDao;
import jdbc_util.FirstApp;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.PayWay;
import java.sql.SQLException;
import java.util.List;

public class PayWayDaoImpl implements IPayWayDao {
    @Override
    public void addPayWay(PayWay payWay) {
        QueryRunner qr  = null;
        try {
            String sql = "insert into payway(paywayname) values(?)";
            qr = new QueryRunner();
            qr.update(FirstApp.getConn(), sql, payWay.getPayWayName());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void updatePayWay(PayWay payWay) {
        QueryRunner qr  = null;
        try {
            String sql = "update payway set paywayname=? where Paywayid =?";
            qr = new QueryRunner();
            qr.update(FirstApp.getConn(), sql,payWay.getPayWayName(),payWay.getPaywayid());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public List<PayWay> findAll() {
        List<PayWay> payWayList = null;
        QueryRunner qr  = null;
        try {
            String sql = "Select * from payway";
            qr = new QueryRunner();
            payWayList = qr.query(FirstApp.getConn(), sql, new BeanListHandler<PayWay>(PayWay.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return payWayList;
    }
}